﻿using LD.BC;
using LD.Host;
using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace LD.Tool
{
    /// <summary>
    /// 光栅停止
    /// </summary>
    public class LightStopHelper
    {
        private static object _lock = new object();
        private static LightStopHelper instance;
        private TaskEx taskEx;
        private bool isPause;
        private LightStopHelper()
        {
            taskEx = new TaskEx();
        }
        public static LightStopHelper Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (_lock)
                    {
                        if (instance == null)
                            instance = new LightStopHelper();
                    }
                }
                return instance;
            }
        }
        public void Start(string flowName)
        {
            Stop();
            taskEx.Start(Pause, flowName);
        }
        public void Stop()
        {
            Log4.Log.Info("开始停止光栅");
            taskEx?.Pause();
            taskEx?.Cancel();
        }
        private void Pause(object obj)
        {
            var flows = new FlowTaskEx[FlowHelper.TaskExs.Count];
            FlowHelper.TaskExs.CopyTo(flows);
            if (flows.Length > 0)
            {
                var gses = BcFactory.Instance.Config.IoList
                            .Where(p => p.IoType == (int)IoType.输入 &&
                                        p.FixType == RunEnum.光栅.ToString())
                            .ToList();
                bool flag = false;
                foreach (var item in gses)
                {
                    var on_off = BcFactory.Instance.Control.GetIn((ushort)item.No);
                    if (on_off == (int)item.ValidateLevel)
                    {
                        flag = true;
                        break;
                    }
                }
                if (flag)
                {
                    isPause = true;
                    foreach (var item in flows)
                    {
                        item.Pause();
                    }
                }
                else
                {
                    isPause = false;
                    foreach (var item in flows)
                    {
                        item.Reset();
                    }
                }
            }

            Task.Delay(5).Wait();
        }
        private void Run(object obj)
        {
            bool pauseTrig = false;
            //光栅
            var pause = BcFactory.Instance.Config.IoList
                    .Where(p => p.IoType == (int)IoType.输入 && p.FixType == RunEnum.光栅.ToString())
                    .FirstOrDefault();
            if (pause != null)
            {
                //检测信号
                var on_off = BcFactory.Instance.Control.GetIn((ushort)pause.No);
                pauseTrig = Convert.ToBoolean(on_off);
                if (pauseTrig && Appbus.StateEnum == StateEnum.自动模式)
                {
                    //所有轴停止
                    BcFactory.Instance.Control.StopALL();

                    BCAlertHelper.Log("光栅", LogLevel.严重);

                    //灯全部关闭
                    BcFactory.Instance.StartStop.ClosedLight();

                    //黄灯
                    BcFactory.Instance.StartStop.Light(LightEnum.黄灯, LevelType.低电平);

                    //通知全局
                    BcFactory.Instance.RunState(RunEnum.光栅);
                }
            }
            Task.Delay(10).Wait();
        }

        public bool IsPause
        {
            get
            {
                return isPause;
            }
        }
        public void PauseWait()
        {
            //判定是否暂停了
            while (isPause &&
                   Appbus.StateEnum == StateEnum.自动模式)
            {
                //暂停等待
                Task.Delay(5).Wait();
            }
        }
    }
}
